package org.scmpatrol.scm.action;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToOne;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.scmpatrol.scm.diff.Diff;
import org.scmpatrol.scm.SCMPath;

@Entity
@DiscriminatorValue("U")
public class SCMModificationAction extends SCMAction implements Serializable {

	private static final long serialVersionUID = 5694263628517630728L;

	boolean copied = false;
	String copiedFrom;
	String copiedFromRevision;
	
	@OneToOne(cascade=CascadeType.ALL)
	Diff diff;

    public SCMModificationAction() {
        super();
    }

	public SCMModificationAction(SCMPath path) {
		super(path);
	}

	public String getCopiedFromRevision() {
		return copiedFromRevision;
	}

	public void setCopiedFromRevision(String copiedFromRevision) {
		if (StringUtils.isNotBlank(copiedFromRevision)) {
			copied = true;
			this.copiedFromRevision = copiedFromRevision;
		} else {
			copied = false;
		}
	}

	public boolean isCopied() {
		return copied;
	}

	public String getCopiedFrom() {
		return copiedFrom;
	}

	public void setCopiedFrom(String copiedFrom) {
		if (StringUtils.isNotBlank(copiedFrom)) {
			copied = true;
			this.copiedFrom = copiedFrom;
		} else {
			copied = false;
		}
	}

	@Override
	public SCMActionTypeEnum getActionType() {
		return SCMActionTypeEnum.UPDATE;
	}
	
	public void setDiff(Diff diff) {
		this.diff = diff;
	}
	
	public Diff getDiff() {
		return diff;
	}
	
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

}
